package com.tancire.system.service.impl;

import java.util.List;
import javax.annotation.Resource;

import com.tancire.common.constant.Constants;
import com.tancire.common.core.redis.RedisCache;
import com.tancire.common.utils.DateUtils;
import org.springframework.stereotype.Service;
import com.tancire.system.mapper.SysAddressLibraryMapper;
import com.tancire.system.domain.SysAddressLibrary;
import com.tancire.system.service.ISysAddressLibraryService;

/**
 * 地址管理Service业务层处理
 *
 * @author tancire
 * @date 2024-03-02
 */
@Service
public class SysAddressLibraryServiceImpl implements ISysAddressLibraryService {

    @Resource
    private SysAddressLibraryMapper sysAddressLibraryMapper;

    @Resource
    private RedisCache redisCache;

    /**
     * 查询地址管理
     *
     * @param id 地址管理主键
     * @return 地址管理
     */
    @Override
    public SysAddressLibrary selectSysAddressLibraryById(String id) {
        return sysAddressLibraryMapper.selectSysAddressLibraryById(id);
    }

    /**
     * 查询地址管理列表
     *
     * @param sysAddressLibrary 地址管理
     * @return 地址管理
     */
    @Override
    public List<SysAddressLibrary> selectSysAddressLibraryList(SysAddressLibrary sysAddressLibrary) {
        return sysAddressLibraryMapper.selectSysAddressLibraryList(sysAddressLibrary);
    }

    /**
     * 新增地址管理
     *
     * @param sysAddressLibrary 地址管理
     * @return 结果
     */
    @Override
    public int insertSysAddressLibrary(SysAddressLibrary sysAddressLibrary) {
        sysAddressLibrary.setCreateTime(DateUtils.getNowDate());
        int count = sysAddressLibraryMapper.insertSysAddressLibrary(sysAddressLibrary);
        // 刷新redis
        this.cacheRedis();
        return count;
    }

    /**
     * 更新redis缓存
     */
    private List<SysAddressLibrary> cacheRedis() {
        SysAddressLibrary query = new SysAddressLibrary();
        query.setIsShow("1");
        query.setDelFlag("0");
        query.setParentId(0L);
        List<SysAddressLibrary> addressList = this.sysAddressLibraryMapper.selectSysAddressLibraryList(query);
        this.redisCache.setCacheObject(Constants.ADDRESS_LIBRARY_KEY, addressList);
        return addressList;
    }

    /**
     * 修改地址管理
     *
     * @param sysAddressLibrary 地址管理
     * @return 结果
     */
    @Override
    public int updateSysAddressLibrary(SysAddressLibrary sysAddressLibrary) {
        sysAddressLibrary.setUpdateTime(DateUtils.getNowDate());
        int count = sysAddressLibraryMapper.updateSysAddressLibrary(sysAddressLibrary);
        this.cacheRedis();
        return count;
    }

    /**
     * 批量删除地址管理
     *
     * @param ids 需要删除的地址管理主键
     * @return 结果
     */
    @Override
    public int deleteSysAddressLibraryByIds(String[] ids) {
        return sysAddressLibraryMapper.deleteSysAddressLibraryByIds(ids);
    }

    /**
     * 删除地址管理信息
     *
     * @param id 地址管理主键
     * @return 结果
     */
    @Override
    public int deleteSysAddressLibraryById(String id) {
        return sysAddressLibraryMapper.deleteSysAddressLibraryById(id);
    }

    /**
     * 获取redis 缓存
     *
     * @return
     */
    @Override
    public Object selectAddressByRedis() {
        Object cacheObject = this.redisCache.getCacheObject(Constants.ADDRESS_LIBRARY_KEY);
        if (null == cacheObject) {
            List<SysAddressLibrary> sysAddressLibraries = this.cacheRedis();
            cacheObject = sysAddressLibraries;
        }
        return cacheObject;
    }
}
